﻿using AntDesign;
using AntDesign.TableModels;
 
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using ERP.Blazor.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.EntityActions;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.TableColumns;
using ERP.Abp.Identity;

namespace ERP.Blazor
{
    public partial class AdminTable111
    {
        private DatePicker<DateTime> date ;
        
        [Inject] IJSRuntime IJSRuntime { get; set; }

        WeatherForecast[] forecasts;
        IEnumerable<WeatherForecast> selectedRows;
        ITable table;
        int _pageIndex = 1;
        int _pageSize = 20;
        int _total = 0;
        string _guid = $"table{DateTime.Now.Ticks}";
        string _scrollY = "600px";
        bool _loading = false;
        bool _visibleModal = false;

        private  String Term { get; set; }
        private void dateChange(DateTimeChangedEventArgs args)
        {
            Term= args.Date.ToString("yyyy年MM");
            StateHasChanged();
        }
        protected override async Task OnInitializedAsync()
        {
            forecasts = await GetForecastAsync(1, 50);
            _total = 50;
            for (var i = 0; i < 10; i++)
            {
                formData.Add($"field-{i}", new FormValue());
            }
        }
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };
        public void query(QueryModel<WeatherForecast> queryModel)
        {

            Console.WriteLine(JsonSerializer.Serialize(queryModel));
        }
        public Task<WeatherForecast[]> GetForecastAsync(int pageIndex, int pageSize)
        {

            var rng = new Random();
            return Task.FromResult(Enumerable.Range((pageIndex - 1) * pageSize + 1, pageSize).Select(index =>
            {
                var temperatureC = rng.Next(-20, 55);
                return new WeatherForecast
                {
                    Id = index,
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = temperatureC,
                    Summary = Summaries[rng.Next(Summaries.Length)],
                    Hot = temperatureC > 30,
                };
            }).ToArray());
        }

        public void RemoveSelection(int id)
        {
            var selected = selectedRows.Where(x => x.Id != id);
            selectedRows = selected;
        }

        private void Delete(int id)
        {
            forecasts = forecasts.Where(x => x.Id != id).ToArray();
            _total = forecasts.Length;
        }
        bool _expand = false;
        AntDesign.Internal.IForm _form;
        Dictionary<string, FormValue> formData = new();

        protected override void OnInitialized()
        {
            base.OnInitialized();
        }
 


        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            base.OnAfterRender(firstRender);
            if (firstRender)
            {
              await   ResetTableHeight();
            }
        }


        async void ExpandSearch()
        {
            _expand = !_expand;
            await ResetTableHeight();
        }
      protected  async Task ResetTableHeight()
        {
            var height1 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $".ant-tabs-nav-wrap", "offsetHeight");
            var height2 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .search", "offsetHeight");
            var height3 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .toolbar", "offsetHeight");
            var height4 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} table>thead", "offsetHeight");
            var height5 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .ant-table-pagination", "offsetHeight");
            var totals = await IJSRuntime.InvokeAsync<int>("dom.getDom", "body", "offsetHeight");
            _scrollY = (totals - height1 - height2 - height3 - height4 - height5 - 48 - 10 - 8 * 2 - 4) + "px";
            StateHasChanged();
        }
        class FormValue
        {
            public string Value { get; set; }
        }


       
    }
}
